﻿using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD
{
    public partial class FrmAlarm : Form
    {
        private LogHelper logger = LogHelper.Instance;
        public FrmAlarm()
        {
            InitializeComponent();
            lv_log.Scrollable = true;
            logger.LogEvent += Logger_LogEvent;
        }

        private void Logger_LogEvent(string content, LogLevel type)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    ListViewItem Item = new ListViewItem();
                    switch (type)
                    {
                        case LogLevel.一般:
                            Item.StateImageIndex = 1;
                            Item.ForeColor = Color.Navy;
                            break;
                        case LogLevel.警告报警:
                            Item.StateImageIndex = 1;
                            Item.ForeColor = Color.OrangeRed;
                            break;
                        case LogLevel.警告:
                            Item.StateImageIndex = 0;
                            Item.ForeColor = Color.OrangeRed;
                            break;

                        case LogLevel.严重:
                            Item.StateImageIndex = 0;
                            Item.ForeColor = Color.Red;
                            break;
                    }

                    Item.Text = DateTime.Now + ":" + content;

                    if (lv_log.Items.Count > 100)
                        lv_log.Items[0].Remove();

                    lv_log.Items.Add(Item);
                    lv_log.Items[lv_log.Items.Count - 1].EnsureVisible();
                }));
            }
        }

        private void lv_log_Resize(object sender, EventArgs e)
        {
            lv_log.Columns[0].Width = this.Width;
        }

        private void 清空列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.lv_log.Items.Clear();
        }
    }
}
